package com.czh.dubbo.consumer;

import com.czh.dubbo.api.DemoService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ConsumerMain {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new
                AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();

        ConsumerComponent consumerComponent = context.getBean(ConsumerComponent.class);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
        scheduledExecutorService.scheduleAtFixedRate(() -> consumerComponent.demoService.methodA("a"),0,50,
                TimeUnit.MILLISECONDS);
        scheduledExecutorService.scheduleAtFixedRate(() -> consumerComponent.demoService.methodB("a"),0,50, TimeUnit.MILLISECONDS);
        scheduledExecutorService.scheduleAtFixedRate(() -> consumerComponent.demoService.methodC("a"),0,50, TimeUnit.MILLISECONDS);

        System.in.read();
        scheduledExecutorService.shutdown();
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "com.czh.dubbo")
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(value = {"com.czh.dubbo.consumer"})
    static class ConsumerConfiguration {
    }
}
